package com.zjsru.plan2023.oneday;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: CookLee
 * @Date: 2023/8/3
 * 删除注释 leetcode:722
 *
 */
public class RemoveComments {
    
    /**
     * 模拟 ，处理不同种类的注释关系即可
     *
     * @param source 源
     * @return {@link List}<{@link String}>
     */
    public List<String> removeComments(String[] source) {
        List<String> res = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        //标记是否注释
        boolean block = false;
        for (String law : source) {
            for (int i = 0; i < law.length(); i++) {
                if (block) {
                    //处理注释结尾，如果是*/ 则改变block状态
                    if (i + 1 < law.length() && law.charAt(i) == '*' && law.charAt(i + 1) == '/') {
                        block = false;
                        i++;
                    }
                } else {
                    //如果是注释/*开头 则改变block状态
                    if (i + 1 < law.length() && law.charAt(i) == '/' && law.charAt(i + 1) == '*') {
                        block = true;
                        i++;
                    } else if (i + 1 < law.length() && law.charAt(i) == '/' && law.charAt(i + 1) == '/') {
                        //如果是// 则忽略
                        break;
                    } else {
                        sb.append(law.charAt(i));
                    }
                }
            }
            if (!block && sb.length() > 0) {
                res.add(sb.toString());
                //将该行字符串清零
                sb.setLength(0);
            }
        }
        return res;
    }
    
    public static void main(String[] args) {
        RemoveComments removeComments = new RemoveComments();
        //["/*Test program */", "int main()", "{ ", "  // variable declaration ", "int a, b, c;", "/* This is a test", "   multiline  ", "   comment for ", "   testing */", "a = b + c;", "}"]
        String[] source = new String[] {"/*Test program */", "int main()", "{ ", "  // variable declaration ",
                "int a, b, c;", "/* This is a test", "   multiline  ", "   comment for ", "   testing */", "a = b + c;",
                "}"};
        System.out.println(removeComments.removeComments(source));
    }
}
